/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * <p>Project: jse2204 - Font2
 * <p>Powered by webrx On 2022-09-26 14:21:15
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Font2 {
    public static void main(String[] args) throws Exception {
        int width = 500;
        int height = 500;
        BufferedImage img = new BufferedImage(width, height, 1);
        Graphics2D g = img.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        String text = "版权所有 电话:123";
        int size = 35;

        Font f = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/zh164hfyh.ttf"));
        Font font = f.deriveFont(Font.BOLD, size);
        g.setFont(font);

        FontMetrics fm = g.getFontMetrics();
        int fw = fm.stringWidth(text);
        int fh = fm.getAscent() - (fm.getHeight() - fm.getAscent());

        g.setColor(Color.RED);

        //BasicStroke stroke = new BasicStroke(13);
        //g.setStroke(stroke);

        //右上角
        int sp = 10;
        int x = sp;
        int y = fh + sp;

        //正中央
        x = (width - fw) / 2;
        y = (height - fh) / 2 + fh;

        //右下角
        x = width - fw - sp;
        y = height - fh + sp;

        //左下角


        g.drawString(text, x, y);


        g.setColor(Color.YELLOW);
        g.drawLine(250, 0, 250, 500);
        g.drawLine(0, 250, 500, 250);


        g.dispose();
        ImageIO.write(img, "jpg", new File("m1.jpg"));

        Runtime.getRuntime().exec("cmd /k start m1.jpg");
    }
}
